import axios from "axios";
import store from "@/store";
import { getToken } from "@/utils";

const service = axios.create({
  baseURL: "http://localhost:9999",
});

service.interceptors.request.use(
  (config) => {
    const token = store.getters.token || getToken();
    if (token) {
      config.headers.Authorization = token;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

service.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default service;
